package com.song.su.controller;


import com.song.su.dto.BookMessage;
import com.song.su.mapper.BookDetailMapper;
import com.song.su.mapper.CommentMapper;
import com.song.su.model.*;
import com.song.su.provider.BookProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.song.su.provider.BookProvider.bookMessageArrayList;
import static com.song.su.provider.BookProvider.do_provide;


@Controller
public class IndexController {

    @Autowired
    BookDetailMapper bookDetailMapper;

    @Autowired
    CommentMapper commentMapper;


        @GetMapping("/")
        public String hello(Model model)  {
            // 推荐
            List<BookDetail> bookDetails = new ArrayList<>();
            bookDetails.add(bookDetailMapper.findBook(String.valueOf(11117)));
            bookDetails.add(bookDetailMapper.findBook(String.valueOf(11116)));
            bookDetails.add(bookDetailMapper.findBook(String.valueOf(11114)));
            bookDetails.add(bookDetailMapper.findBook(String.valueOf(11115)));

            model.addAttribute("books",bookDetails);
            //排行
            try {
                List<BookDetail> bookDetail = new ArrayList<>();
                List<PinglunRank> pinglunRanks = commentMapper.pinglunPaihang();
                for (int i = 0; i < 4; i++) {
                    bookDetail.add(bookDetailMapper.findBook(pinglunRanks.get(i).getBookid()));
                }

                model.addAttribute("book_pinglunRank",bookDetail);
                model.addAttribute("pinglun",pinglunRanks);


                List<BookDetail> bookDetail_like = new ArrayList<>();
                List<likeRank>  likeRanks= commentMapper.like_count();
                for (int i = 0; i < 4; i++) {
                    bookDetail_like.add(bookDetailMapper.findBook(likeRanks.get(i).getBookid()));
                }

                model.addAttribute("book_likeRank",bookDetail_like);
                model.addAttribute("likeRank",likeRanks);


                List<Footer> footers = commentMapper.footer();
                model.addAttribute("footer",footers);
                SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                List<String> date = new ArrayList<>();
                for (int i = 0; i <6; i++) {
                    //获取评论日期
                    date.add(dateformat.format(footers.get(i).getGmt_create()).split(" ")[0]);
                }
                model.addAttribute("date",date);

                System.out.println(date.get(0));
//                System.out.println(bookDetail.size());
            } catch (Exception e) {
                e.printStackTrace();
            }


            return "index";
        }

}
